<template>
	<view class="search">
		<!-- 输入框 -->
		<view class="search-box">
			<!-- uni-search-bar 这个组件的样式去uni_modules文件下的找到来，然后在里面进行修改 -->
			<uni-search-bar :focus="true" @confirm="search" @input="input" :radius="100" cancelButton="none"
				v-model="searchValue"></uni-search-bar>
		</view>
		<!-- 关键字建议列表 -->
		<view class="sugg-list">
			<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoGoods(item.goods_id)">
				<view class="sugg-content">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 输入的历史记录 -->
		<view class="histroy-box">
			<!-- 搜索历史的标题区 -->
			<view class="histroy-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clearHistroy"></uni-icons>
			</view>
			<!-- 所搜历史的列表 -->
			<view class="histroy-list">
				<uni-tag :text="item" v-for="(item,i) in history" :key="i" circle="true" inverted="true" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchValue: '',
				timer: null,
				searchResults: [],
				historyList: []
			};
		},

		computed: {
			history() {
				return [...this.historyList].reverse()
			}
		},

		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.searchValue = e
					this.getSearchResult()
				}, 500)
			},

			addHistoryList() {
				// 去重
				// 将数组转化为set对象
				const set = new Set(this.historyList)
				// 2. 调用 Set 对象的 delete 方法，移除对应的元素
				set.delete(this.searchValue)
				// 3. 调用 Set 对象的 add 方法，向 Set 中添加元素
				set.add(this.searchValue)
				// 4. 将 Set 对象转化为 Array 数组
				this.historyList = Array.from(set)

				uni.setStorageSync('searchValue', JSON.stringify(this.historyList))

			},

			async getSearchResult() {
				if (this.searchValue.trim() === '') {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.searchValue
				})

				if (res.meta.status !== 200) return uni.$showMes()
				this.searchResults = res.message

				this.addHistoryList()
			},

			gotoGoods(goods_id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},
			
			clearHistroy() {
				this.historyList = [],
				uni.setStorageSync('searchValue', [])
			},
			
			gotoGoodsList(keyword) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + keyword				})
			}
		},

		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('searchValue') || '[]')
		}
	}
</script>

<style lang="scss">
	uni-search-bar {
		display: flex;
		flex-direction: row;
		background-color: #007aff;
		padding: 16rpx;
		position: relative;
	}

	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}


	.sugg-item {
		display: flex;
		padding: 15px 0;
		border: 1px solid #efefef;
		justify-content: space-between
	}

	.sugg-content {
		// 不允许换行
		white-space: nowrap;
		// 溢出隐藏
		overflow: hidden;
		// 隐藏用...代替
		text-overflow: ellipsis;
		margin-right: 5px;
	}

	.histroy-title {
		height: 27px;
		padding: 2px 6px;
		font-size: 12px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: #efefef;
	}
</style>
